package com.yun.article.controller;

import com.heima.model.article.dtos.ApArticleDTO;
import com.heima.model.article.dtos.ArticleHomeDTO;
import com.heima.model.common.dtos.ResponseResult;
import com.yun.article.enums.LoadEnum;
import com.yun.article.service.ApArticleService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description: 文章接口
 * @author: yu
 * @data 2025年07月18日 15:25
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/article")
@RequiredArgsConstructor
public class ArticleHomeController {

    private final ApArticleService apArticleService;


    /**
     * @description: 加载列表
     * @title: load
     * @param: [articleHomeDTO]
     */
    @PostMapping("/load")
    public ResponseResult load(@RequestBody ArticleHomeDTO articleHomeDTO) {
        return apArticleService.load(articleHomeDTO, LoadEnum.LOAD_DEFAULT.getLoad());
    }

    @PostMapping("/loadmore")
    public ResponseResult loadMore(@RequestBody ArticleHomeDTO articleHomeDTO) {
        return apArticleService.load(articleHomeDTO, LoadEnum.LOAD_MAX.getLoad());
    }

    @PostMapping("/loadnew")
    public ResponseResult loadNew(@RequestBody ArticleHomeDTO articleHomeDTO) {
        return apArticleService.load(articleHomeDTO, LoadEnum.LOAD_MIN.getLoad());
    }

    /**
     * @description: 保存文章
     * @title: insert
     * @param: [apArticleDTO]
     */
    @PostMapping("/save")
    public ResponseResult insert(@RequestBody ApArticleDTO apArticleDTO) {
        return apArticleService.insertArticle(apArticleDTO);
    }

    /**
     * @description: 根据文章id下架文章
     * @title: updateArticleDown
     * @param: [articleId]
     */
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value = "article.config.is-down.queue", durable = "true"),   // 声明队列
                    exchange = @Exchange(value = "article.config.direct.exchange", type = "direct"), // 声明交换机
                    key = "article.config.update"  // 绑定关系
            )
    )
    public void updateArticleDown(Long articleId) {
        log.info("下架文章是{}", articleId);
        apArticleService.articleDown(articleId);
    }

}
